<?php

class CRUDS{

	private $table;
	private $select_arr = "";
	private $from_arr = "";
	private $where_arr = "";
	private $join_arr = "";
	private $on_join_arr = "";
	private $inner_join_arr = "";
	private $on_inner_join_arr = "";
	
	function set_table($tbl){$this->table = $tbl;}
	
	function insert($arr){
		$columns = '';
		$fill = '';
		foreach($arr as $key => $value){
			$columns .= $key;
			$columns .= ', ';
			
			$fill .= $value;
			$fill .= ', ';
		}
		
		$columns = substr($columns, 0, strlen($columns)-2);
		$fill = substr($fill, 0, strlen($fill)-2);
		
		$sql = "INSERT INTO ".$this->table." (".$columns.") VALUES (".$fill.")";
		$query = mysql_query($sql);
		
		echo $sql;
	}
	
	function update($arr_set, $arr_params, $tbl){
		$set = '';
		
		foreach($arr_set as $key => $value){
			$set .= $key;
			$set .= '=';
			$set .= "'".$value."'";
			$set .= ', ';
		}
		
		$set = substr($set, 0, strlen($set)-2);
	
		$params = '';
		
		foreach($arr_params as $key => $value){
			$params .= $key;
			$params .= '=';
			$params .= "'".$value."'";
			$params .= ', ';
		}
		
		$params = substr($params, 0, strlen($params)-2);
		
		$sql = "UPDATE ".$tbl." SET ".$set. " WHERE ".$params;
		
		mysql_query($sql);
		
		echo $sql;
	}
	
	function delete($del){
		
		$sql = "DELETE FROM ".$del." WHERE ".$this->where_arr;
		$this->where_arr = "";
		
		mysql_query($sql);
	}
	
	function select($param){
		$this->select_arr .= $param;
	}
	
	function from($param){
		$this->from_arr .= $param;
	}
	
	function where($param){
		$this->where_arr .= $param;
	}
	
	function join($table_param, $content){
		$this->join_arr .= $table_param;
		$this->on_join_arr .= $content;
	}
	
	function inner_join($table_param, $content){
		$this->inner_join_arr .= $table_param;
		$this->on_inner_join_arr .= $content;
	}
	
	function get(){
		$query = "SELECT ";
		$query .= $this->select_arr;
		$query .= " FROM ";
		$query .= $this->from_arr;
		if($this->inner_join_arr != "" && $this->on_inner_join_arr != ""){
			$query .= " INNER JOIN ";
			$query .= $this->inner_join_arr;
			$query .= " ON ";
			$query .= $this->on_inner_join_arr;
		}
		if($this->join_arr != "" && $this->on_join_arr != ""){
			$query .= " JOIN ";
			$query .= $this->join_arr;
			$query .= " ON ";
			$query .= $this->on_join_arr;
		}
		if($this->where_arr != ""){
			$query .= " WHERE ";
			$query .= $this->where_arr;
		}
		
		$this->select_arr = "";
		$this->from_arr = "";
		$this->inner_join_arr = "";
		$this->join_arr = "";
		$this->where_arr = "";
		
		//return $query;
		return mysql_query($query);
	}
	
	function check(){
		$query = "SELECT ";
		$query .= $this->select_arr;
		$query .= " FROM ";
		$query .= $this->from_arr;
		if($this->inner_join_arr != "" && $this->on_inner_join_arr != ""){
			$query .= " INNER JOIN ";
			$query .= $this->inner_join_arr;
			$query .= " ON ";
			$query .= $this->on_inner_join_arr;
		}
		if($this->join_arr != "" && $this->on_join_arr != ""){
			$query .= " JOIN ";
			$query .= $this->join_arr;
			$query .= " ON ";
			$query .= $this->on_join_arr;
		}
		if($this->where_arr != ""){
			$query .= " WHERE ";
			$query .= $this->where_arr;
		}
		
		$this->select_arr = "";
		$this->from_arr = "";
		$this->inner_join_arr = "";
		$this->join_arr = "";
		$this->where_arr = "";
		
		return $query;
		
	}

}

?>